package org.edu.modules.portal.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;

import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.util.TimeStampUtil;
import org.edu.modules.portal.entity.VeSiteBook;
import org.edu.modules.portal.service.IVeSiteBookService;
import org.edu.modules.portal.vo.VeSiteBookAddVO;
import org.edu.modules.portal.vo.VeSiteBookReplyVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Api(tags = {"在线留言"})
@RestController
@RequestMapping({"/portal/veSiteBook"})
@ApiSort(100)
public class VeSiteBookController
        extends BaseController<VeSiteBook, IVeSiteBookService> {
    private static final Logger log = LoggerFactory.getLogger(org.edu.modules.portal.controller.VeSiteBookController.class);


    @Autowired
    private IVeSiteBookService veSiteBookService;


    @AutoLog("在线留言-分页查询")
    @ApiOperation(value = "分页查询", notes = "在线留言-分页查询")
    @PostMapping({"/list"})
    @ApiOperationSupport(order = 1)
    public Result<?> queryPageList(VeSiteBook veSiteBook, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<VeSiteBook> queryWrapper = QueryGenerator.initQueryWrapper(veSiteBook, req.getParameterMap());
        Page<VeSiteBook> page = new Page(pageNo.intValue(), pageSize.intValue());
        IPage<VeSiteBook> pageList = this.veSiteBookService.page((IPage) page, (Wrapper) queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog("在线留言-通过id查询")
    @ApiOperation(value = "通过id查询", notes = "在线留言-通过id查询")
    @PostMapping({"/queryById"})
    @ApiOperationSupport(order = 2)
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeSiteBook veSiteBook = (VeSiteBook) this.veSiteBookService.getById(id);
        if (veSiteBook == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veSiteBook);
    }


    @AutoLog("在线留言-新增信息")
    @ApiOperation(value = "新增留言", notes = "在线留言-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeSiteBookAddVO veSiteBookAddVO) {
        VeSiteBook veSiteBook = new VeSiteBook();
        BeanUtils.copyProperties(veSiteBookAddVO, veSiteBook);
        veSiteBook.setAddtime(Integer.valueOf(TimeStampUtil.getNowSecondStamp()));
        if (this.veSiteBookService.save(veSiteBook)) {
            return Result.OK("添加成功！");
        }
        return Result.error("操作失败");
    }


    @AutoLog("在线留言-回复留言")
    @ApiOperation(value = "回复留言", notes = "在线留言-回复留言")
    @PostMapping({"/reply"})
    public Result<?> reply(@RequestBody VeSiteBookReplyVO veSiteBookReplyVO) {
        VeSiteBook veSiteBook = new VeSiteBook();
        BeanUtils.copyProperties(veSiteBookReplyVO, veSiteBook);
        veSiteBook.setReplytime(Integer.valueOf(TimeStampUtil.getNowSecondStamp()));
        if (this.veSiteBookService.updateById(veSiteBook)) {
            return Result.OK("回复成功!");
        }
        return Result.error("操作失败");
    }


    @AutoLog("在线留言-通过id删除")
    @ApiOperation(value = "通过id删除", notes = "在线留言-通过id删除")
    @PostMapping({"/delete"})
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        if (this.veSiteBookService.removeById(id)) {
            return Result.OK("删除成功!");
        }
        return Result.error("操作失败");
    }


    @AutoLog("在线留言-批量删除")
    @ApiOperation(value = "批量删除", notes = "在线留言-批量删除")
    @PostMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        if (this.veSiteBookService.removeByIds(Arrays.asList(ids.split(",")))) {
            return Result.OK("批量删除成功!");
        }
        return Result.error("操作失败");
    }


    @AutoLog("多信息列表-审核通过")
    @PostMapping({"auditPass"})
    @ApiOperation(value = "审核通过", notes = "多信息列表-审核通过")
    public Result<?> auditPass(@RequestParam(name = "ids", required = true) String ids) {
        if (this.veSiteBookService.auditPass(ids.split(",")) > 0) {
            return Result.OK("发布成功!");
        }
        return Result.error("操作失败");
    }


    @AutoLog("多信息列表-审核不通过")
    @PostMapping({"auditNoPass"})
    @ApiOperation(value = "审核不通过", notes = "多信息列表-审核不通过")
    public Result<?> auditNoPass(@RequestParam(name = "ids", required = true) String ids) {
        if (this.veSiteBookService.auditNoPass(ids.split(",")) > 0) {
            return Result.OK("取消发布成功!");
        }
        return Result.error("操作失败");
    }
}


